package com.test.personalblogproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.personalblogproject.mapper.FavoritesCollectionMapper;
import com.test.personalblogproject.mapper.UserMapper;
import com.test.personalblogproject.pojo.FavoritesCollection;
import com.test.personalblogproject.pojo.Result;
import com.test.personalblogproject.pojo.User;
import com.test.personalblogproject.service.FavoritesCollectionService;
import com.test.personalblogproject.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FavoritesCollectionServiceImpl extends ServiceImpl<FavoritesCollectionMapper, FavoritesCollection> implements FavoritesCollectionService {
    @Autowired
    private UserMapper userMapper;




    @Override
    public Result createNewList(String name, int privateOrNot) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User sysUser = userMapper.findByUserName(username);
        FavoritesCollection favoritesCollection=new FavoritesCollection();
        favoritesCollection.setUserId(String.valueOf(sysUser.getId()));
        favoritesCollection.setName(name);
        favoritesCollection.setPrivateornot(privateOrNot);
        favoritesCollection.setCreateTime(new Date());
        save(favoritesCollection);
        return Result.success("添加收藏列表成功");

    }

    @Override
    public Result viewAllCollection() {

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User sysUser = userMapper.findByUserName(username);
        String userId=String.valueOf(sysUser.getId());
        LambdaQueryWrapper<FavoritesCollection> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FavoritesCollection::getUserId,userId);
        List<FavoritesCollection> list=list(lambdaQueryWrapper);
        return Result.success(list);

    }
}
